//
//  XGL_PersonalBottomView.swift
//  XGamesLive
//
//  Created by ios on 2024/5/17.
//

import UIKit
import JXSegmentedView

protocol PersonalBottomDelegate: NSObjectProtocol {
    func didSelectedSegmentView(segmentView: PanSegmentView)
}

class XGL_PersonalBottomView: UIView {
    weak var delegate: PersonalBottomDelegate?
    var segmentIndex: NSInteger = 0
    lazy var hudView: XGL_CustomHudView = XGL_CustomHudView()
    let segmentedDataSource = JXSegmentedTitleDataSource()
    let segmentedView = JXSegmentedView()
    lazy var listContainerView: JXSegmentedListContainerView! = {
        return JXSegmentedListContainerView(dataSource: self)
    }()
    var tableviewArr:[XGL_PersonalVideoSegmentView] = []
    lazy var myPostSegmentView: XGL_PersonalVideoSegmentView = XGL_PersonalVideoSegmentView()
    lazy var likePostSegmentView: XGL_PersonalVideoSegmentView = XGL_PersonalVideoSegmentView()
    
    private var titles: [String] = []
    
    init(titles: [String]) {
        super.init(frame: CGRect.zero)
        self.titles = titles
        self.myPostSegmentView.segmentType = 1
        self.likePostSegmentView.segmentType = 2
        self.setSegmentView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
extension XGL_PersonalBottomView{
    func setSegmentView() {
        if titles.count < 2{
            self.tableviewArr.append(self.myPostSegmentView)
        }else{
            self.tableviewArr.append(self.myPostSegmentView)
            self.tableviewArr.append(self.likePostSegmentView)
        }
        //配置数据源
        segmentedDataSource.isTitleColorGradientEnabled = true
        segmentedDataSource.titles = titles
        //reloadData(selectedIndex:)一定要调用
        segmentedDataSource.titleSelectedColor = UIColor.white
        segmentedDataSource.titleNormalColor = UIColor.white.withAlphaComponent(0.5)
        segmentedDataSource.titleNormalFont = UIFont.pingfangSC_Medium(fontSize: 16.0)
        segmentedDataSource.titleSelectedFont = UIFont.pingfangSC_Medium(fontSize: 16.0)
        segmentedDataSource.reloadData(selectedIndex: 0)
        
        //配置指示器
        let indicator = JXSegmentedIndicatorLineView()
        indicator.indicatorWidth = JXSegmentedViewAutomaticDimension
        indicator.lineStyle = .lengthen
        indicator.indicatorColor = UIColor.hexColor("#80FFE6")
        //segmentedViewDataSource一定要通过属性强持有！！！！！！！！！
        segmentedView.dataSource = segmentedDataSource
        segmentedView.indicators = [indicator]
        segmentedView.delegate = self
        
        self.addSubview(segmentedView)
        segmentedView.snp.makeConstraints { (make) in
            make.top.equalToSuperview()
            make.left.equalToSuperview()
            if titles.count > 1{
                make.width.equalToSuperview().multipliedBy(0.5)
            }else{
                make.width.equalToSuperview().multipliedBy(0.25)
            }
            make.height.equalTo(48)
        }
        segmentedView.contentScrollView = listContainerView.scrollView
        self.addSubview(listContainerView)
        listContainerView.snp.makeConstraints { (make) in
            make.centerX.width.bottom.equalToSuperview()
            make.top.equalTo(segmentedView.snp.bottom)
        }
    }
}

extension XGL_PersonalBottomView: JXSegmentedListContainerViewListDelegate {
    func listView() -> UIView {
        return self
    }
}

extension XGL_PersonalBottomView: JXSegmentedViewDelegate {
    func segmentedView(_ segmentedView: JXSegmentedView, didClickSelectedItemAt index: Int) {
        listContainerView.didClickSelectedItem(at: index)
        self.segmentIndex = index
    }
    
    func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {
        listContainerView.didClickSelectedItem(at: index)
        self.delegate?.didSelectedSegmentView(segmentView: self.tableviewArr[index])
    }
    
}

extension XGL_PersonalBottomView: JXSegmentedListContainerViewDataSource {
    func numberOfLists(in listContainerView: JXSegmentedListContainerView) -> Int {
        if let titleDataSource = segmentedView.dataSource as? JXSegmentedBaseDataSource {
            return titleDataSource.dataSource.count
        }
        return 0
    }
    
    func listContainerView(_ listContainerView: JXSegmentedListContainerView, initListAt index: Int) -> JXSegmentedListContainerViewListDelegate {
        return self.tableviewArr[index]
    }
}
